home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / BasicTreeCellRenderer.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  307 lines

  1. /*
  2.  * @(#)BasicTreeCellRenderer.java    1.18 98/03/05
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.basic;
  22.  
  23. import com.sun.java.swing.*;
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.beans.*;
  27. import java.io.*;
  28. import java.util.*;
  29. import com.sun.java.swing.tree.*;
  30.  
  31. /**
  32.  * Displays an entry in a tree.
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  * @version 1.18 03/05/98
  41.  * @author Rob Davis
  42.  * @author Ray Ryan
  43.  * @author Scott Violet
  44.  */
  45. public class BasicTreeCellRenderer extends JLabel implements TreeCellRenderer
  46. {
  47.     /** Is the value currently selected. */
  48.     protected boolean selected;
  49.  
  50.     // Icons
  51.     /** Icon used to show non-leaf nodes that aren't expanded. */
  52.     transient protected Icon closedIcon;
  53.  
  54.     /** Icon used to show leaf nodes. */
  55.     transient protected Icon leafIcon;
  56.  
  57.     /** Icon used to show non-leaf nodes that are expanded. */
  58.     transient protected Icon openIcon;
  59.  
  60.     // Colors
  61.     /** Color to use for the foreground for selected nodes. */
  62.     protected Color textSelectionColor;
  63.  
  64.     /** Color to use for the foreground for non-selected nodes. */
  65.     protected Color textNonSelectionColor;
  66.  
  67.     /** Color to use for the background when a node is selected. */
  68.     protected Color backgroundSelectionColor;
  69.  
  70.     /** Color to use for the background when the node isn't selected. */
  71.     protected Color backgroundNonSelectionColor;
  72.  
  73.     /** Color to use for the background when the node isn't selected. */
  74.     protected Color borderSelectionColor;
  75.  
  76.     /**
  77.       * Returns a new instance of BasicTreeCellRenderer.  Alignment is
  78.       * set to left aligned.
  79.       */
  80.     public BasicTreeCellRenderer() {
  81.     setHorizontalAlignment(JLabel.LEFT);
  82.  
  83.     setLeafIcon(UIManager.getIcon("Tree.leafIcon"));
  84.     setClosedIcon(UIManager.getIcon("Tree.closedIcon"));
  85.     setOpenIcon(UIManager.getIcon("Tree.openIcon"));
  86.  
  87.     setTextSelectionColor(UIManager.getColor("Tree.textSelectionColor"));
  88.     setTextNonSelectionColor(UIManager.getColor("Tree.textNonSelectionColor"));
  89.     setBackgroundSelectionColor(UIManager.getColor("Tree.backgroundSelectionColor"));
  90.     setBackgroundNonSelectionColor(UIManager.getColor("Tree.backgroundNonSelectionColor"));
  91.     setBorderSelectionColor(UIManager.getColor("Tree.borderSelectionColor"));
  92.     }
  93.  
  94.  
  95.     /**
  96.       * Returns the default icon used to represent non-leaf nodes that are expanded.
  97.       */
  98.     public Icon getDefaultOpenIcon() {
  99.     return openIcon;
  100.     }
  101.  
  102.     /**
  103.       * Returns the default icon used to represent non-leaf nodes that are not
  104.       * expanded.
  105.       */
  106.     public Icon getDefaultClosedIcon() {
  107.     return closedIcon;
  108.     }
  109.  
  110.     /**
  111.       * Returns the default icon used to represent leaf nodes.
  112.       */
  113.     public Icon getDefaultLeafIcon() {
  114.     return leafIcon;
  115.     }
  116.  
  117.     /**
  118.       * Sets the icon used to represent non-leaf nodes that are expanded.
  119.       */
  120.     public void setOpenIcon(Icon newIcon) {
  121.     openIcon = newIcon;
  122.     }
  123.  
  124.     /**
  125.       * Returns the icon used to represent non-leaf nodes that are expanded.
  126.       */
  127.     public Icon getOpenIcon() {
  128.     return openIcon;
  129.     }
  130.  
  131.     /**
  132.       * Sets the icon used to represent non-leaf nodes that are not expanded.
  133.       */
  134.     public void setClosedIcon(Icon newIcon) {
  135.     closedIcon = newIcon;
  136.     }
  137.  
  138.     /**
  139.       * Returns the icon used to represent non-leaf nodes that are not
  140.       * expanded.
  141.       */
  142.     public Icon getClosedIcon() {
  143.     return closedIcon;
  144.     }
  145.  
  146.     /**
  147.       * Sets the icon used to represent leaf nodes.
  148.       */
  149.     public void setLeafIcon(Icon newIcon) {
  150.     leafIcon = newIcon;
  151.     }
  152.  
  153.     /**
  154.       * Returns the icon used to represent leaf nodes.
  155.       */
  156.     public Icon getLeafIcon() {
  157.     return leafIcon;
  158.     }
  159.  
  160.     /**
  161.       * Sets the color the text is drawn with when the node is selected.
  162.       */
  163.     public void setTextSelectionColor(Color newColor) {
  164.     textSelectionColor = newColor;
  165.     }
  166.  
  167.     /**
  168.       * Returns the color the text is drawn with when the node is selected.
  169.       */
  170.     public Color getTextSelectionColor() {
  171.     return textSelectionColor;
  172.     }
  173.  
  174.     /**
  175.       * Sets the color the text is drawn with when the node isn't selected.
  176.       */
  177.     public void setTextNonSelectionColor(Color newColor) {
  178.     textNonSelectionColor = newColor;
  179.     }
  180.  
  181.     /**
  182.       * Returns the color the text is drawn with when the node isn't selected.
  183.       */
  184.     public Color getTextNonSelectionColor() {
  185.     return textNonSelectionColor;
  186.     }
  187.  
  188.     /**
  189.       * Sets the color to use for the background if node is selected.
  190.       */
  191.     public void setBackgroundSelectionColor(Color newColor) {
  192.     backgroundSelectionColor = newColor;
  193.     }
  194.  
  195.  
  196.     /**
  197.       * Returns the color to use for the background if node is selected.
  198.       */
  199.     public Color getBackgroundSelectionColor() {
  200.     return backgroundSelectionColor;
  201.     }
  202.  
  203.     /**
  204.       * Sets the background color to be used for non selected nodes.
  205.       */
  206.     public void setBackgroundNonSelectionColor(Color newColor) {
  207.     backgroundNonSelectionColor = newColor;
  208.     }
  209.  
  210.     /**
  211.       * Returns the background color to be used for non selected nodes.
  212.       */
  213.     public Color getBackgroundNonSelectionColor() {
  214.     return backgroundNonSelectionColor;
  215.     }
  216.  
  217.     /**
  218.       * Sets the color to use for the border.
  219.       */
  220.     public void setBorderSelectionColor(Color newColor) {
  221.     borderSelectionColor = newColor;
  222.     }
  223.  
  224.     /**
  225.       * Returns the color the border is drawn.
  226.       */
  227.     public Color getBorderSelectionColor() {
  228.     return borderSelectionColor;
  229.     }
  230.  
  231.  
  232.     /**
  233.       * Configures the renderer based on the passed in components.
  234.       * The value is set from messaging value with toString().
  235.       * The foreground color is set based on the selection and the icon
  236.       * is set based on on leaf and expanded.
  237.       */
  238.     public Component getTreeCellRendererComponent(JTree tree, Object value,
  239.                           boolean sel,
  240.                           boolean expanded,
  241.                           boolean leaf, int row,
  242.                           boolean hasFocus) {
  243.     String         stringValue = tree.convertValueToText(value, sel,
  244.                       expanded, leaf, row, hasFocus);
  245.     setFont( tree.getFont() );
  246.     setText(stringValue);
  247.     if(sel)
  248.         setForeground(getTextSelectionColor());
  249.     else
  250.         setForeground(getTextNonSelectionColor());
  251.     if (leaf) {
  252.         setIcon(getLeafIcon());
  253.     } else if (expanded) {
  254.         setIcon(getOpenIcon());
  255.     } else {
  256.         setIcon(getClosedIcon());
  257.     }
  258.         
  259.     selected = sel;
  260.  
  261.     return this;
  262.     }
  263.  
  264.     /**
  265.       * Paints the value.  The background is filled based on selected.
  266.       */
  267.     public void paint(Graphics g) {
  268.     Color bColor;
  269.  
  270.     if(selected) {
  271.         bColor = getBackgroundSelectionColor();
  272.     } else {
  273.         bColor = getBackgroundNonSelectionColor();
  274.         if(bColor == null)
  275.         bColor = getBackground();
  276.     }
  277.     if(bColor != null) {
  278.         Icon currentI = getIcon();
  279.  
  280.         g.setColor(bColor);
  281.         if(currentI != null && getText() != null) {
  282.         int offset = (currentI.getIconWidth() + getIconTextGap());
  283.  
  284.         g.fillRect(offset, 0, getWidth() - 1 - offset,
  285.                getHeight() - 1);
  286.         } else {
  287.         g.fillRect(0, 0, getWidth()-1, getHeight()-1);
  288.         }
  289.     }
  290.     if (selected) {
  291.         g.setColor(getBorderSelectionColor());
  292.         g.drawRect(0, 0, getWidth()-1, getHeight()-1);
  293.     }
  294.     super.paint(g);
  295.     }
  296.  
  297.     public Dimension getPreferredSize() {
  298.     Dimension        retDimension = super.getPreferredSize();
  299.  
  300.     if(retDimension != null)
  301.         retDimension = new Dimension(retDimension.width + 3,
  302.                      retDimension.height);
  303.     return retDimension;
  304.     }
  305.  
  306. }
  307.